import { generatorDynamicRouter } from '@/router/generate-routers'
import { getSystemMenu } from '@/apis/modules/baseApi'

export default {
  namespaced: true,
  state: {
    asyncRoutes: [],

  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.asyncRoutes = routers
    },
  },
  actions: {
    //调用后台接口得到可访问菜单列表,生成路由
    generateRoutes({ commit }) {
      return new Promise((resolve, reject) => {
        getSystemMenu().then(res => {
          generatorDynamicRouter(res.data).then(routers => {
            commit('SET_ROUTERS', routers)
            resolve(true)
          }).catch(error => reject(error))
        }).catch(error => reject(error))
      })
    },

  }
}